﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;
using System.Collections.Generic;
using Microsoft.Practices.Unity;
using LCA.Community.DAL.Test.ModelFactories;

namespace LCA.Community.DAL.Test
{
    /// <summary>
    ///This is a test class for PhrasesRepositoryTest and is intended
    ///to contain all PhrasesRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class PhrasesRepositoryTest : BaseTestClass
    {
        private List<User> _users;
        private UsersFactory _usersFactory = new UsersFactory();
        private List<Phrase> _phrases;
        private PhrasesFactory _phraseFactory = new PhrasesFactory();

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            _users = _usersFactory.CreateList(5);
            _users.ForEach(u => usersRepository.Add(u));

            var phrasesRepository = _unityContainer.Resolve<IPhrasesRepository>();
            Phrase phrase;
            _phrases = new List<Phrase>();

            phrase = _phraseFactory.CreateEntity(_users[0].RowKey, _users[1].RowKey);
            _phrases.Add(phrase);
            phrasesRepository.Add(phrase);

            phrase = _phraseFactory.CreateEntity(_users[0].RowKey, _users[2].RowKey);
            _phrases.Add(phrase);
            phrasesRepository.Add(phrase);

            phrase = _phraseFactory.CreateEntity(_users[0].RowKey, _users[3].RowKey);
            _phrases.Add(phrase);
            phrasesRepository.Add(phrase);

            phrase = _phraseFactory.CreateEntity(_users[0].RowKey, _users[4].RowKey);
            _phrases.Add(phrase);
            phrasesRepository.Add(phrase);
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            var phrasesRepository = _unityContainer.Resolve<IPhrasesRepository>();

            usersRepository.DeleteTestEntities();
            phrasesRepository.DeleteTestEntities();
        }


        /// <summary>
        ///A test for DeleteAllUserPhrases
        ///</summary>
        [TestMethod()]
        public void DeleteAllUserPhrasesTest()
        {
            var target = _unityContainer.Resolve<IPhrasesRepository>();
            string userID = _users[0].RowKey;
            target.DeleteAllUserPhrases(userID);

            var actual = target.GetPhrasesByUser(userID);
            CollectionAssert.AreEquivalent(new List<Phrase>(), actual);
        }

        /// <summary>
        ///A test for GetPhrasesByUser
        ///</summary>
        [TestMethod()]
        public void GetPhrasesByUserTest()
        {
            var target = _unityContainer.Resolve<IPhrasesRepository>();
            string userID = _users[0].RowKey;
            List<Phrase> expected = _phrases;
            List<Phrase> actual;

            actual = target.GetPhrasesByUser(userID);
            CollectionAssert.AreEquivalent(expected, actual);
        }
    }
}
